<?php

/**
 * This is the model class for table "products".
 *
 * The followings are the available columns in table 'products':
 * @property string $id
 * @property string $code
 * @property string $name
 * @property string $category_id
 * @property double $price
 * @property string $description
 * @property string $images
 * @property integer $status
 * @property string $available_sizes
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $deleted
 *
 * The followings are the available model relations:
 * @property Orders[] $orders
 * @property Categories $category
 */
class Product extends CActiveRecord implements IECartPosition
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'products';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('code, name, category_id', 'required'),
			array('status, create_time, update_time', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('deleted', 'boolean'),
			array('code, name, available_sizes', 'length', 'max'=>50),
			array('category_id', 'length', 'max'=>10),
			array('images', 'length', 'max'=>250),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, code, name, category_id, price, description, images, status, available_sizes, create_time, update_time, deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'orders' => array(self::MANY_MANY, 'Order', 'order_details(product_id, order_id)'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('labels','ID'),
			'code' => Yii::t('product','Mã sản phẩm'),
			'name' => Yii::t('product','Tên sản phẩm'),
			'category_id' => Yii::t('product','Loại sản phẩm'),
			'price' => Yii::t('product','Giá'),
			'description' => Yii::t('labels','Mô tả'),
			'images' => Yii::t('product','Hình ảnh'),
			'status' => Yii::t('product','Trạng thái'),
			'available_sizes' => Yii::t('product','Size hiện có'),
			'create_time' => Yii::t('labels','Ngày tạo'),
			'update_time' => Yii::t('labels','Ngày sửa'),
			'deleted' => Yii::t('labels','Xóa'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('images',$this->images,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('available_sizes',$this->available_sizes,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('deleted',$this->deleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function behaviors(){
		return array(
			'CTimestampBehavior' => array(
				'class' => 'zii.behaviors.CTimestampBehavior',
			)
		);
	}
	
	function getId(){
        return 'Product'.$this->id;
    }

    function getPrice(){
        return $this->price;
    }
}